﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HttpRequester;
using mshtml;
using Common.Constants;
using BotvaBot.BusinessLayer.RequestChainItems.Factory;

namespace BotvaBot.RequestChainItems
{
    [ChainItem]
    public class OtmenitWorkOnFermaChainItem : BotvaLogedInChainItemBase
    {
        protected override bool IsContextValidInternal()
        {
            return base.IsContextValidInternal();
        }

        protected override void PrepareRequestInternal()
        {
            Url = "http://g3.botva.ru/farm.php";
            Method = HttpMethod.POST;

            string content = Chain.LastContent;
            content = content.Replace(Environment.NewLine, string.Empty);
            while (content.IndexOf("  ") >= 0)
            {
                content = content.Replace("  ", " ");
            }
            content = content.ToUpper();

            IHTMLDocument2 html = new HTMLDocumentClass();
            html.write(content);
            html.close();

            IHTMLElementCollection all = html.body.all as IHTMLElementCollection;

            IHTMLElementCollection inputs = all.tags("INPUT") as IHTMLElementCollection;
            foreach (IHTMLElement item in inputs)
            {
                IHTMLInputHiddenElement inputHidden = item as IHTMLInputHiddenElement;
                if (inputHidden == null)
                    continue;

                if (inputHidden.name == "K")
                {
                    AddPost(inputHidden.name.ToLower(), inputHidden.value.ToLower());
                    break;
                }

                //if (inputHidden.name == "CMD")
                //    rm.AddPost(inputHidden.name.ToLower(), inputHidden.value.ToLower());
            }

            AddPost("cmd", "cancel");
        }

        public override bool IsResponseValidInternal()
        {
            //string content = Requester.PageContent;
            //content = content.Replace(" ", string.Empty)
            //                .Replace("'", "\"")
            //                .ToUpper();

            //string searchedStr = "<input type='submit' class='cmd_all cmd_medium_sl cmd_amedium_sl ' value='ВХОД' >";
            //searchedStr = searchedStr.Replace(" ", string.Empty)
            //                .Replace("'", "\"")
            //                .ToUpper();

            //return content.Contains(searchedStr);

            return true;
        }
    }
}
